Add chaff to message blocks. blocks is a list of 3-tuples of the
form (serial-number, data, MAC).
Chaff is created by choosing a random number of the same
byte-length as data, and another random number of the same
byte-length as MAC. The message block's serial number is
placed on the chaff block and all the packet's chaff blocks
are randomly interspersed with the single wheat block. This
method then returns a list of 3-tuples of the same form.
Chaffed blocks will contain multiple instances of 3-tuples
with the same serial number, but the only way to figure out
which blocks are wheat and which are chaff is to perform the
MAC hash and compare values.
"""
chaffedblocks = []
count = len(blocks) * self._Chaff__factor
blocksper = range(self._Chaff__blocksper)
for i, wheat in map(None, range(len(blocks)), blocks):
if i < count:
(serial, data, mac) = wheat
datasize = len(data)
macsize = len(mac)
addwheat = 1
for j in blocksper:
import sys
chaffdata = self._randnum(datasize)
chaffmac = self._randnum(macsize)
chaff = (serial, chaffdata, chaffmac)
if addwheat and bytes_to_long(self._randnum(16)) & 64:
chaffedblocks.append(wheat)
addwheat = 0
chaffedblocks.append(chaff)
if addwheat:
chaffedblocks.append(wheat)
addwheat
chaffedblocks.append(wheat)
return chaffedblocks
def _randnum(self, size):
randpool = randpool
import Crypto.Util
import time
pool = randpool.RandomPool(size * 2)
while size > pool.entropy:
pass
return pool.get_bytes(size)
if __name__ == '__main__':
text = 'We hold these truths to be self-evident, that all men are created equal, that\nthey are endowed by their Creator with certain unalienable Rights, that among\nthese are Life, Liberty, and the pursuit of Happiness. That to secure these\nrights, Governments are instituted among Men, deriving their just powers from\nthe consent of the governed. That whenever any Form of Government becomes\ndestructive of these ends, it is the Right of the People to alter or to\nabolish it, and to institute new Government, laying its foundation on such\nprinciples and organizing its powers in such form, as to them shall seem most\nlikely to effect their Safety and Happiness.\n'
print 'Original text:\n=========='
print text
print '=========='
blocks = []
size = 40
for i in range(0, len(text), size):
blocks.append(text[i:i + size])
print 'Calculating MACs...'
from Crypto.Hash import HMAC, SHA
key = 'Jefferson'
macs = [ HMAC.new(key, block, digestmod = SHA).digest() for block in blocks ]
if not len(blocks) == len(macs):
raise AssertionError
source = []
m = map(None, range(len(blocks)), blocks, macs)
print m
for i, data, mac in m:
source.append((i, data, mac))
print 'Adding chaff...'
c = Chaff(factor = 0.5, blocksper = 2)
chaffed = c.chaff(source)
from base64 import encodestring
wheat = []
print 'chaffed message blocks:'
for i, data, mac in chaffed:
h = HMAC.new(key, data, digestmod = SHA)
pmac = h.digest()
if pmac == mac:
tag = '-->'
wheat.append(data)
else:
tag = ' '
print tag, '%3d' % i, repr(data), encodestring(mac)[:-1]